import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.swing.plaf.basic.BasicIconFactory;

import org.junit.Test;

/**
 * This is develop
 * 
 * Project Name：java7demo
 * ClassName：BigDecimalTest
 * Description：
 * @author: wupengwei
 * @date: 2018年11月27日 下午7:55:01
 * note:
 *
 */
public class BigDecimalTest {
	
	@Test
	public void test1() {
		System.out.println(0.2 + 0.1);
        System.out.println(0.3 - 0.1);
        System.out.println(0.2 * 0.1);
        System.out.println(0.3 / 0.1);
	}

	@Test
	public void test2() {
		double d1 = 0.2 + 0.1;
		double d2 = 0.3;
		System.out.println("d1=" + d1);
		System.out.println("d2=" + d2);
		System.out.println(d1 == d2);
		
		int compareTo = new Double(d1).compareTo(d2);//不可行
		System.out.println(compareTo);
		
		int compareTo2 = new BigDecimal(d1).compareTo(new BigDecimal(d2));
		System.out.println(compareTo2);
		
		System.out.println(Math.abs(d1 - d2) < 0.000001);
		
	}
	
	@Test
	public void test3() {
		BigDecimal add = new BigDecimal(0.2).add(new BigDecimal(0.1));
		System.out.println(add);
		System.out.println(add.compareTo(new BigDecimal(0.3)));
	}
	
	@Test
	public void test4() {
		float a = (float)0.1;
	    float b = (float)0.1;
	    if(a == b)
	        System.out.println("a == b");
	    else
	    	System.out.println("a != b");

	    float c = (float)0.1;
	    double d = 0.1;//java中小数默认是double类型
	    if(c == d)
	        System.out.println("c == d");
	    else
	    	System.out.println("c != d");

	    float e = (float)0.1;
	    double f = 0.1;
	    if(Math.abs(e - f) < 0.0001)
	        System.out.println("e == f");
	    else
	    	System.out.println("e != f");

	}
	
	@Test
	public void test5() {
		try {
			Integer i = null;
					i.intValue();
		} catch (Exception e) {
			System.out.println(e);
			//e.printStackTrace();
		}
		
	}
	
	@Test
	public void test6() {
		LongAnI l = new LongAnI();
		int i = 0;
		l.setId(i == 0 ? 0L : 1L);
		System.out.println(l.getId());
	}
	
	/**
	 * 构造函数传double值，结果是无法预知的，
	 * @Description：
	 * @author: wupengwei
	 * @date: 2018年10月22日 下午9:57:21
	 */
	@Test
	public void test7() {
		BigDecimal b = new BigDecimal(0.1);
		BigDecimal b2 = BigDecimal.valueOf(0.1);
		System.out.println(b);//0.1000000000000000055511151231257827021181583404541015625
		System.out.println(b2);//0.1
		
	}
	
	@Test
	public void test8() {
		BigDecimal a = BigDecimal.valueOf(0.1);
		BigDecimal b = BigDecimal.valueOf(0.2);
		BigDecimal add = a.add(b);
		BigDecimal subtract = a.subtract(b);
		BigDecimal multiply = a.multiply(b);
		BigDecimal divide = a.divide(b);
		
		System.out.println("add = " + add);
		System.out.println("subtract = " + subtract);
		System.out.println("multiply = " + multiply);
		System.out.println("divide = " + divide);
		
	}
	
	@Test
	public void test9() {
		BigDecimal a = BigDecimal.valueOf(0.1555);
		BigDecimal b = BigDecimal.valueOf(1);
		BigDecimal divide = a.divide(b, 2 , BigDecimal.ROUND_HALF_UP);
		System.out.println("divide = " + divide);
		
		BigDecimal a2 = BigDecimal.valueOf(0.1555);
		BigDecimal b2 = BigDecimal.valueOf(0.17);
		BigDecimal divide2 = a2.divide(b2, 2 , BigDecimal.ROUND_HALF_UP);
		System.out.println("divide = " + divide2);
		
		BigDecimal divide3 = new BigDecimal("4.6").divide(new BigDecimal("1.16"), 3, BigDecimal.ROUND_DOWN);
		System.out.println("============" + divide3);
	
	}
	
	@Test
	public void test10() {
		BigDecimal a = new BigDecimal("4.5635");

        a = a.setScale(3, RoundingMode.HALF_UP);    //保留3位小数，且四舍五入
        System.out.println(a);
	}
	
	@Test
	public void test90() {
		Double a = 0.1;
		Double b = 0.2;
		Double c = a + b;
//		Double.doubleToLongBits(0.01) == Double.doubleToLongBits(0.01)
		System.out.println(Double.doubleToLongBits(c));
		System.out.println(Double.doubleToLongBits(0.3));
		System.out.println(Double.doubleToLongBits(c) == Double.doubleToLongBits(0.3));
	}
		
	@Test
	public void test11() {
		BigDecimal a = new BigDecimal("0.0000");
		System.out.println(a.equals(BigDecimal.ZERO));
		System.out.println(a.compareTo(BigDecimal.ZERO) == 0);
	}
	
	@Test
	public void test12() {
		this.testArray(null);
	}

	@Test
	public void test13() {
		
		System.out.println(new BigDecimal(7).divide(new BigDecimal(1.16), 3 ,BigDecimal.ROUND_UP));
		System.out.println(new BigDecimal(4.6).divide(new BigDecimal(1.16), 3 ,BigDecimal.ROUND_UP));
	}
	
	private void testArray(Integer[] array) {
		if (array == null) {
			System.out.println("kong");
		}
	}
	
}
class LongAnI {
	
	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	
}